package topic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.MyLogUtils;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/3/24.
 */

@ContentView(R.layout.deliver_comment)
public class DeliverComment extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.deliver_blog_back)
    private ImageView deliver_blog_back;
    @ViewInject(R.id.deliver_blog_finish)
    private TextView deliver_blog_finish;
    @ViewInject(R.id.deliver_blog_number)
    private TextView deliver_blog_number;
    @ViewInject(R.id.deliver_blog_content)
    private EditText deliver_blog_content;

    private String content;//输入的内容
    private String blogid = "";
    private String commentid = "";//一级评论的id
    private String commenterid = "";//被评论者的id
    private String nickname;

    private boolean hasComment ;


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject((String) msg.obj);
                        if ("0".equals(jsonObject.getString("err_code"))) {
                            hasComment = true;
                            Intent intent = new Intent();
                            intent.putExtra("hasComment",hasComment);
                            DeliverComment.this.setResult(2,intent);
                            DeliverComment.this.finish();
                        } else {
                            ToastUtils.shortToast(jsonObject.getString("msg"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        blogid = getIntent().getStringExtra("blogid");
        commentid = getIntent().getStringExtra("commentid");
        commenterid = getIntent().getStringExtra("commenterid");
        nickname = getIntent().getStringExtra("nickname");
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        deliver_blog_finish.setOnClickListener(this);
        deliver_blog_back.setOnClickListener(this);
        if (nickname != null) {
            deliver_blog_content.setHint("回复 " + nickname + " : ");
        }
        deliver_blog_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 255) {
                    deliver_blog_content.setText(content);
                    deliver_blog_content.setSelection(content.length());
                    ToastUtils.shortToast("输入字数已达上限");
                } else {
                    content = s.toString();
                }
                deliver_blog_number.setText("" + content.length());
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.deliver_blog_finish:
                if (!TextUtils.isEmpty(content)) {
                    CommitComment();
                }
                break;
            case R.id.deliver_blog_back:
                this.finish();
                break;
            default:
                break;
        }
    }


    private void CommitComment() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("content", content);
        hashMap.put("blogid", blogid);
        if (!TextUtils.isEmpty(commentid)) {
            hashMap.put("commentid", commentid);
        }
        if (!TextUtils.isEmpty(commenterid)) {
            hashMap.put("commenterid", commenterid);
        }
        new InterNetController(this, Constant.COMMENTBLOG, handler, hashMap, 0);
    }
}
